package cn.mntool.fastcode.generate.utils;

import cn.mntool.fastcode.generate.config.Properties;
import cn.mntool.fastcode.generate.entity.BeanInfo;
import cn.mntool.fastcode.generate.entity.FieldInfo;

import static cn.mntool.fastcode.generate.constants.DatabaseType.MYSQL;
import static cn.mntool.fastcode.generate.constants.DatabaseType.ORACLE;
import static cn.mntool.fastcode.generate.constants.LabelConstants.*;

/**
 * describe: 文本替换工具
 *
 * @author lianying
 * @date 2019/11/13
 */
public class StringUtils {
    /**
     * 字符串截取
     *
     * @param target 源
     * @param begin  开始字符串
     * @param end    结束字符串
     * @return
     */
    public static String subStringContent(String target, String begin, String end) {
        int beginIndex = target.indexOf(begin);
        int endIndex = target.indexOf(end);
        if (beginIndex == -1) {
            return null;
        }
        return target.substring(beginIndex + begin.length(), endIndex);
    }

    /**
     * 字符替换
     *
     * @param fileContent
     * @param beanInfo
     * @param properties
     * @return
     */
    public static String replaceForClass(String fileContent, BeanInfo beanInfo, Properties properties) {
        //数据处理
        fileContent = fileContent.replace(PACKAGE_NAME, properties.getPackageName());
        fileContent = fileContent.replace(PK_JAVA_NAME, beanInfo.getPkJavaName());
        fileContent = fileContent.replace(PK_NAME, beanInfo.getPkName());
        fileContent = fileContent.replace(LOW_CASE_CLASS_NAME, StringUtils.lowCamel(properties.getClassName()));
        fileContent = fileContent.replace(UP_CASE_CLASS_NAME, StringUtils.upCamel(properties.getClassName()));
        fileContent = fileContent.replace(TABLE_COMMENT, beanInfo.getTableComment());
        fileContent = fileContent.replace(TABLE_NAME, properties.getTableName());
        fileContent = fileContent.replace(AUTHOR, properties.getAuthor());
        fileContent = fileContent.replace(TIME, properties.getTime());
        return fileContent;
    }

    /**
     * 字符替换
     *
     * @param fileContent
     * @param fieldInfo
     * @return
     */
    public static String replaceForField(String fileContent, FieldInfo fieldInfo) {
        //数据处理
        fileContent = fileContent.replace(LOW_CASE_FIELD_NAME, StringUtils.lowCamel(fieldInfo.getFieldName()));
        fileContent = fileContent.replace(UP_CASE_FIELD_NAME, StringUtils.upCamel(fieldInfo.getFieldName()));
        fileContent = fileContent.replace(FIELD_JAVA_TYPE, fieldInfo.getFieldJavaType());
        fileContent = fileContent.replace(FIELD_COMMENT, fieldInfo.getFieldComment());
        fileContent = fileContent.replace(DB_FIELD_NAME, fieldInfo.getDbFieldName());
        return fileContent;
    }

    /**
     * 字符串大驼峰式处理，例如TestHello
     *
     * @param s
     * @return
     */
    public static String upCamel(String s) {
        //以下划线分割。进行驼峰处理
        String value = "";
        String[] items = s.split("_");
        for (int i = 0; i < items.length; i++) {
            value += items[i].substring(0, 1).toUpperCase() + items[i].substring(1);
        }
        return value;
    }

    /**
     * 字符串小驼峰式处理，例如testHello
     *
     * @param s
     * @return
     */
    public static String lowCamel(String s) {
        //以下划线分割。进行驼峰处理
        String value = "";
        String[] items = s.split("_");
        for (int i = 0; i < items.length; i++) {
            if (i == 0) {
                value += items[i].substring(0, 1).toLowerCase() + items[i].substring(1);
            } else {
                value += items[i].substring(0, 1).toUpperCase() + items[i].substring(1);
            }
        }
        return value;
    }

    /**
     * 数据库类型转换成Java类型
     *
     * @param dataType
     * @return
     */
    public static String toJavaType(String databaseType, String dataType) {
        //TODO 类型对应还未完善
        switch (databaseType) {
            case MYSQL:
                return toJavaTypeByMysql(dataType);
            case ORACLE:
                return toJavaTypeByOracle(dataType);
        }
        throw new RuntimeException("不支持的数据库类型");
    }

    /**
     * mysql数据类型转换成java数据类型
     *
     * @param dataType 数据库数据类型
     */
    private static String toJavaTypeByMysql(String dataType) {
        switch (dataType) {
            case "tinyint":
            case "boolean":
            case "big":
                return "Boolean";
            case "smallint":
            case "mediumint":
            case "int":
            case "integer":
                return "Integer";
            case "bigint":
            case "id":
                return "Long";
            case "float":
            case "double":
            case "decimal":
                return "Double";
            case "blob":
                return "byte[]";
            case "year":
                return "java.sql.Date";
            case "time":
                return "java.sql.Time";
            case "date":
                return "java.util.Date";
            case "datetime":
            case "timestamp":
                return "java.sql.Timestamp";
            default:
                return "String";
        }
    }

    /**
     * oracle数据类型转换成java数据类型
     *
     * @param dataType 数据库数据类型
     */
    private static String toJavaTypeByOracle(String dataType) {
        switch (dataType) {
            case "NUMBER":
                return "Double";
            case "DATE":
                return "java.util.Date";
            default:
                return "String";
        }
    }
}
